/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.file;

import org.vnetcon.blobdb.database.SQLTransactionException;
import org.vnetcon.blobdb.database.backend.KeyInterface;
import org.vnetcon.blobdb.database.backend.TableDataInterface;
import org.vnetcon.blobdb.database.backend.file.fdo.TableData;

public class FileTableData implements TableDataInterface, Comparable<FileTableData> {
	TableData nativeTable = null;
	
	public FileTableData(TableData nativeTable){
		this.nativeTable = nativeTable;
	}
	
	@Override
	public byte[] getBytes(boolean forWrite) throws SQLTransactionException {
		byte[] b = this.nativeTable.getDataRows(forWrite);
		return b;
	}

	@Override
	public int getFreeRows() {
		return this.nativeTable.getFreeRows();
	}

	@Override
	public void setLastRowid(int lastRowid) {
		this.nativeTable.setLastRowid(lastRowid);
	}

	@Override
	public void setDataRows(byte[] rows) throws SQLTransactionException {
		this.nativeTable.setDataRows(rows);
	}

	@Override
	public int getLastRowid() {
		return this.nativeTable.getLastRowid();
	}

	@Override
	public byte[] getDataRows(boolean forWrite) throws SQLTransactionException {
		byte[] b = this.nativeTable.getDataRows(forWrite);
		return b;
	}

	@Override
	public int getTableID() {
		return this.nativeTable.getTableID();
	}

	@Override
	public int getTableDataID() {
		return this.nativeTable.getTableDataID();
	}

	@Override
	public void setRowCount(int rowcount) {
		this.nativeTable.setRowCount(rowcount);
	}

	@Override
	public void setFreeRows(int freerows) {
		this.nativeTable.setFreeRows(freerows);
	}

	@Override
	public int getRowCount() {
		return this.nativeTable.getRowCount();
	}

	@Override
	public void touch(long l) {
		this.nativeTable.touch(l);
	}

	@Override
	public TableDataInterface newInstance(int newTableDataID, int updateTableID) {
		TableData ta = new TableData(newTableDataID, updateTableID);
		FileTableData gaeta = new FileTableData(ta);
		return gaeta;
	}

	@Override
	public KeyInterface getKey() {
		FileKey retKey = new FileKey();
		retKey.setKeyValue(this.nativeTable.getTableDataKey());
		return retKey;
	}

	@Override
	public void setTableKey(KeyInterface key) {
		String nativeKey = (String)key.getKeyValue();
		this.nativeTable.setTableDataKey(nativeKey);
	}

	@Override
	public String getNameForKey() {
		return TableData.class.getSimpleName();
	}

	@Override
	public Object getNativeTable() {
		return this.nativeTable;
	}

	@Override
	public int compareTo(FileTableData o) {
		int iRet = 0;
		
		if(this.nativeTable.getFreeRows() < ((TableData)o.getNativeTable()).getFreeRows()){
			return 1;
		}

		if(this.nativeTable.getFreeRows() > ((TableData)o.getNativeTable()).getFreeRows()){
			return -1;
		}
		
		return iRet;
	}


}
